# add swig java
find_package(SWIG REQUIRED)

find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)
include_directories(${JNI_INCLUDE_DIRS})

include(UseSWIG)

# set package
set(CMAKE_SWIG_FLAGS -package com.artosyn.ar8030)
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_SOURCE_DIR}/com/artosyn/ar8030)

# build base
swig_add_library(ar8030_java
    TYPE SHARED
    LANGUAGE java
    SOURCES ar8030.i
)

target_include_directories(ar8030_java PRIVATE ${JAVA_INCLUDE_PATH})
target_include_directories(ar8030_java PRIVATE ./)
swig_link_libraries(ar8030_java ${JAVA_LIBRARIES} ar8030_client)

# install so
install(TARGETS ar8030_java
    DESTINATION java
)

# origin dll
get_target_property(ar8030_client_type ar8030_client TYPE)

if(ar8030_client_type STREQUAL "SHARED_LIBRARY")
    INSTALL(TARGETS ar8030_client
        DESTINATION java
    )
endif()

# build extend callback
set_property(SOURCE callback.i PROPERTY CPLUSPLUS ON)
swig_add_library(ar8030_callback_java
    TYPE SHARED
    LANGUAGE java
    SOURCES callback.i
)
target_include_directories(ar8030_callback_java PRIVATE ./)
swig_link_libraries(ar8030_callback_java ${JAVA_LIBRARIES} ar8030_client)

# install wrap jni shared library
install(TARGETS ar8030_callback_java
    DESTINATION java
)

# build & install wrap.jar
get_property(ar8030_java_file TARGET ar8030_java PROPERTY SWIG_SUPPORT_FILES)
get_property(ar8030_cb_java_file TARGET ar8030_callback_java PROPERTY SWIG_SUPPORT_FILES)
add_jar(ar8030jni SOURCES ${ar8030_cb_java_file} ${ar8030_java_file} OUTPUT_NAME ar8030_java)
add_dependencies(ar8030jni ar8030_callback_java)
add_dependencies(ar8030jni ar8030_java)
install_jar(ar8030jni DESTINATION java)

if(NOT ANDROID)
    # build example java
    add_jar(
        testjar
        SOURCES main.java
        ENTRY_POINT main
    )
    add_dependencies(testjar ar8030jni ar8030cbjni)
    install_jar(testjar DESTINATION java)
endif()
